package com.study.mike.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.study.mike.common.utils.CookieUtil;
import com.study.mike.common.utils.RedisUtil;
import com.study.mike.common.utils.SessionIdUtil;
import com.study.mike.model.entity.User;
import com.study.mike.service.UserService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author kh
 */
@RequestMapping("view")
@RestController
public class ViewController {
    @Resource
    private HttpServletRequest request;
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private UserService userService;

    @RequestMapping("/test")
    public ModelAndView test(HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/error/500");
        User user = new User();
        user.setAge(15);
        user.setName("康辉");
        session = request.getSession();
        session.setAttribute("user", user);
        return modelAndView;
    }

    @RequestMapping("/login")
    public ModelAndView login(@RequestBody String userName, String password) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/user/login");
        return modelAndView;
    }

    @RequestMapping("/loginIn")
    public boolean loginIn(@RequestBody String userName, String password) {
        System.out.println(userName);
        System.out.println(password);
        return true;
    }

    @RequestMapping("/testSession")
    public void testSession(HttpServletRequest request) {
        //建Session对象
        HttpSession session = request.getSession(true);
        // 获取到会话的创建时间
        System.out.println(session.getCreationTime());
        // 获取到了sessionID;
        String sessionId = session.getId();
        System.out.println(sessionId);
        System.out.println(session + "session对象");
        // 获取会话的cookie
        Cookie[] cookies = request.getCookies();
        System.out.println(cookies.length);
        for (Cookie cookie : cookies) {
            System.out.println(cookie);
        }
//        int[] a = {1,2,3,4,5,};
//
//        redisUtil.set(sessionId, a);
    }

    @RequestMapping("/session")
    public ModelAndView test(HttpServletRequest request, HttpServletResponse response) {
        String sessionId = SessionIdUtil.getSessionId(request);
        System.out.println("session：" + sessionId);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/user/index");
        return modelAndView;
    }

    @RequestMapping("/session1")
    public String test1(HttpServletRequest request, HttpServletResponse response) {
        String sessionId = SessionIdUtil.getSessionId(request);
        System.out.println("session1：" + sessionId);
        System.out.println("hhh");
        return null;
    }

    @GetMapping("/sessionUtil")
    public void sessionUtils(HttpServletResponse response, HttpServletRequest request) {
//        String sessionId = SessionIdUtil.getSessionId(request);
//        String sessionId1 = request.getSession().getId();
//        Cookie[] cookie = request.getCookies();
//        System.out.println("cookie的value：" + cookie[0].getValue());
//        System.out.println("工具类生成：" + sessionId);
//        System.out.println("普通请求获取：" + sessionId);
//        System.out.println(cookie[0].getName());
    }

    @GetMapping("/cookieUtils")
    public void cookieUtils(HttpServletResponse response, HttpServletRequest request) {
        String sessionId = SessionIdUtil.getSessionId(request);
////        String sessionId1 = request.getSession().getId();
////        CookieUtil cookieUtil = new CookieUtil();

//
//
//
//        Cookie[] cookie = request.getCookies();
//        System.out.println("cookie的value：" + cookie[0].getValue());
//        System.out.println("工具类生成：" + sessionId);
//        System.out.println("普通请求获取：" + sessionId);
//        System.out.println(cookie[0].getName());


    }

    @GetMapping("/login")
    public void login(HttpServletRequest request, HttpServletResponse response) {
        String sessionId = SessionIdUtil.getSessionId(request);
        // 登录存入缓存
        List<User> users = userService.queryInfo();
        CookieUtil.writeLoginToken(sessionId, response);
//        Cookie[] cookies = request.getCookies();
//        for (Cookie cookie : cookies) {
//            if (StrUtil.equals(cookie.getName(), "officematew")) {
//                System.out.println(cookie.getName() + "||||" + cookie.getValue());
//            }
//        }
        redisUtil.set(sessionId, users);
    }

    @GetMapping("/logout")
    public void logout(HttpServletRequest request, HttpServletResponse response) {
        //退出登录
        String sessionId = SessionIdUtil.getSessionId(request);
        CookieUtil.writeLoginToken(sessionId, response);
        String seId = CookieUtil.readLoginToken(request);
        if (StrUtil.isBlank(seId)) {
            System.out.println("用户未曾登录！");
        } else {
            //删除session
            CookieUtil.deleteLoginToken(request, response);
            //删除缓存
            redisUtil.del(seId);
            System.out.println("注销成功");
        }
    }

    @GetMapping("/hello")
    public String hello() {
        return "hello";
    }


}
